package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
)

type UsergoodsRBody struct {
	Data       []models.Usergoodsql `json:"data"`
	PageNo     int                  `json:"pageNo"`
	PageSize   int                  `json:"pageSize"`
	TotalCount int64                `json:"totalCount"`
	TotalPage  int64                `json:"totalPage"`
}

type UsergoodsBody struct {
	Page     int    `json:"page"`
	Pagesize int    `json:"pagesize"`
	State    int64  `json:"state" gorm:"state"`   // 1直接购买 2扭蛋机 3一番赏 4魔晶兑换 5vip领取
	Status   int64  `json:"status" gorm:"status"` // 1待确认 2待发货 3已发货4已锁定 5已分解 6已下架
	Phone    string `json:"phone" gorm:"phone"`   // 手机号
}

func Usergoods(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	state := request.State
	status := request.Status
	phone := request.Phone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Usergoodsql
	res := data_source.Db.Model(&models.Usergood{}).Select("usergood.*, user.phone, user.nickname").Joins("left join user on user.id = usergood.uid")
	if status > 0 {
		res = res.Where("usergood.status = ?", status)
	}
	if state > 0 {
		res = res.Where("usergood.state = ?", state)
	}
	if phone != "" {
		res = res.Where("user.phone = ?", phone)
	}
	res.Model(&models.Usergood{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UsergoodsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type UsergoodareasRBody struct {
	Data       []models.Usergoodareasql `json:"data"`
	PageNo     int                      `json:"pageNo"`
	PageSize   int                      `json:"pageSize"`
	TotalCount int64                    `json:"totalCount"`
	TotalPage  int64                    `json:"totalPage"`
}

type UsergoodareasBody struct {
	Page        int    `json:"page"`
	Pagesize    int    `json:"pagesize"`
	Kdstatus    int64  `json:"kdstatus" gorm:"kdstatus"` // 快递状态 1待发货 2已发货 3部分发货
	Phone       string `json:"phone" gorm:"phone"`       // 手机号
	Usergoodids string `json:"usergoodids" gorm:"usergoodids"`
	ID          int64  `json:"id" gorm:"id"`
	Kdorder     string `json:"kdorder" gorm:"kdorder"`
	Kuaidi      string `json:"kuaidi" gorm:"kuaidi"`
}

func Goodfh(ctx *gin.Context) {
	request := UsergoodareasBody{}
	ctx.ShouldBind(&request)
	usergoodids := request.Usergoodids
	id := request.ID
	kdorder := request.Kdorder
	kuaidi := request.Kuaidi

	usergoodidarr := strings.Split(usergoodids, ",")
	usergoodupdate := models.Usergood{
		Status:     3,
		Shouhuo:    1,
		Kuaidi:     kuaidi,
		Kdorder:    kdorder,
		Updatetime: time.Now().Unix(),
	}
	data_source.Db.Where("id in (?)", usergoodidarr).Updates(&usergoodupdate)

	var usergoods []models.Usergood
	data_source.Db.Where("usergoodareaid = ?", id).Find(&usergoods)
	fahuo := 1
	for _, u := range usergoods {

		if u.Status == 2 {
			fahuo = 2
		}

	}
	if fahuo == 1 {
		data_source.Db.Model(&models.Usergoodarea{}).Where("id = ?", id).Update("kdstatus", 2)
	} else {
		data_source.Db.Model(&models.Usergoodarea{}).Where("id = ?", id).Update("kdstatus", 3)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Goodfhqx(ctx *gin.Context) {
	request := UsergoodareasBody{}
	ctx.ShouldBind(&request)
	usergoodids := request.Usergoodids
	id := request.ID

	usergoodidarr := strings.Split(usergoodids, ",")
	usergoodupdate := models.Usergood{
		Status:     2,
		Shouhuo:    3,
		Kuaidi:     " ",
		Kdorder:    " ",
		Updatetime: time.Now().Unix(),
	}
	data_source.Db.Where("id in (?)", usergoodidarr).Updates(&usergoodupdate)

	var usergoods []models.Usergood
	data_source.Db.Where("usergoodareaid = ?", id).Find(&usergoods)
	fahuo := 1
	for _, u := range usergoods {
		if u.Status == 3 {
			fahuo = 2
		}
	}
	if fahuo == 1 {
		data_source.Db.Model(&models.Usergoodarea{}).Where("id = ?", id).Update("kdstatus", 1)
	} else {
		data_source.Db.Model(&models.Usergoodarea{}).Where("id = ?", id).Update("kdstatus", 3)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Usergoodareas(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	state := request.State
	status := request.Status
	phone := request.Phone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Usergoodarea

	var users []models.Usergoodareasql
	res := data_source.Db.Model(&models.Usergoodarea{}).Select("usergoodarea.*, user.phone as userphone").Joins("left join user on user.id = usergoodarea.uid")
	//res := data_source.Db
	if status > 0 {
		res = res.Where("usergoodarea.status = ?", status)
	}
	if state > 0 {
		res = res.Where("usergoodarea.state = ?", state)
	}
	if phone != "" {
		res = res.Where("usergoodarea.phone = ? or user.phone = ?", phone, phone)
	}
	res.Model(&models.Usergoodarea{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var areaidarr []int64
	for _, v := range users {
		areaidarr = append(areaidarr, v.ID)
	}
	var usergoodlist []models.Usergood
	//var kuaidis []models.Kuaidi
	data_source.Db.Where("usergoodareaid in (?)", areaidarr).Find(&usergoodlist)

	for k, v := range users {
		var usergoods []models.Usergood
		for _, u := range usergoodlist {
			if v.ID == u.Usergoodareaid {
				usergoods = append(usergoods, u)
			}
		}
		users[k].Usergood = usergoods
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UsergoodareasRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
